<template>
  <CustomDialog :drag-dialog="dialogInfo" style="z-index: 1201">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      :disabled="!form.busPageInfo.editable"
      size="mini"
      label-width="160px"
    >
      <el-form-item label="名称" prop="busMainData.name">
        <el-input v-model="form.busMainData.name" />
      </el-form-item>
      <el-form-item label="性别" prop="busMainData.gender">
        <el-select
          v-model="form.busMainData.gender"
          filterable
          clearable
          placeholder="性别"
        >
          <el-option
            v-for="item in dicts.gender"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="手机" prop="busMainData.phone">
        <el-input v-model="form.busMainData.phone" />
      </el-form-item>
      <el-form-item label="电话" prop="busMainData.tel">
        <el-input v-model="form.busMainData.tel" />
      </el-form-item>
      <el-form-item label="邮件 " prop="busMainData.email">
        <el-input v-model="form.busMainData.email" />
      </el-form-item>
      <el-form-item label="地址" prop="busMainData.address">
        <el-input v-model="form.busMainData.address" />
      </el-form-item>
      <el-form-item label="职位" prop="busMainData.job">
        <el-input v-model="form.busMainData.job" />
      </el-form-item>
      <el-form-item label="下次联系时间" prop="busMainData.nextContactTime">
        <el-date-picker
          v-model="form.busMainData.nextContactTime"
          placeholder="下次联系时间"
          type="date"
          size="mini"
          clearable
          style="width: 200px"
          class="filter-item"
        />
      </el-form-item>
      <el-form-item label="是否决策人" prop="busMainData.decisionFlag">
        <el-radio
          v-for="item in dicts.enable_flag"
          :key="item.id"
          v-model="form.busMainData.decisionFlag"
          :label="item.dictCode"
          >{{ item.dictName }}</el-radio
        >
      </el-form-item>
      <el-form-item label="最后更新时间" prop="busMainData.lastUpdateDate">
        <el-date-picker
          v-model="form.busMainData.lastUpdateDate"
          :disabled="true"
          placeholder="最后更新时间"
          type="date"
          size="mini"
          clearable
          style="width: 200px"
          class="filter-item"
        />
      </el-form-item>
    </el-form>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button
        v-permission="[
          'SYS_ADMIN',
          'COMMON_ADMIN',
          'CRM_CONTACT_ALL',
          'CRM_CONTACT_CREATE',
        ]"
        :loading="loading"
        type="primary"
        @click="doSave"
        >保存</el-button
      >
    </div>
  </CustomDialog>
</template>

<script>
import contact from "@/api/crm/Contact";
import { isvalidPhone } from "@/utils/validate";

export default {
  components: {},
  dictTypes: ["gender", "enable_flag"],
  props: {
    sup_this: {
      type: Object,
      required: true,
    },
  },
  data() {
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入电话"));
      } else if (!isvalidPhone(value)) {
        callback(new Error("请输入正确的11位手机号码"));
      } else {
        callback();
      }
    };
    return {
      isAdd: false,
      loading: false,
      style: "width: 184px",
      dialogInfo: {
        name: "contactMngFormDialog",
        title: "联系人管理",
        fullScreen: false,
        width: 500,
        height: 90,
      },
      form: {
        busMainData: {
          id: "",
          enableFlag: "Y",
        },
        busPageInfo: {
          editable: false,
        },
      },
      rules: {
        "busMainData.name": [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "长度在 3 到 50 个字符",
            trigger: "blur",
          },
        ],
        // 'busMainData.phone': [
        //   { required: true, trigger: 'blur', validator: validPhone }
        // ]
      },
    };
  },
  methods: {
    cancel() {
      this.resetForm();
    },
    initDataOfBefore() {},
    displayDialog() {
      const { name } = this.dialogInfo;
      this.$nextTick(() => {
        this.$modal.show(name);
      });
    },
    hiddenDialog() {
      const { name } = this.dialogInfo;
      this.$nextTick(() => {
        this.$modal.hide(name);
      });
    },
    addData() {
      this.isAdd = true;
      const data = { paramBean: {} };
      contact
        .toAdd(data)
        .then((res) => {
          this.displayDialog();
          this.form = res.data;
          this.setObjectIdAndCode();
        })
        .catch((err) => {
          console.log(err);
        });
    },
    copyData(mainId) {
      this.isAdd = true;
      const data = { paramBean: { id: mainId } };
      contact
        .toCopy(data)
        .then((res) => {
          this.displayDialog();
          this.form = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    viewMainData(id) {
      this.isAdd = false;
      const data = { paramBean: { id: id } };
      contact.viewMainData(data).then((res) => {
        this.displayDialog();
        this.form = res.data;
      });
    },
    doSubmit() {
      this.doSingleSave(true);
    },
    doSave() {
      this.doSingleSave(false);
    },
    doSingleSave(isSubmit) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit;
          this.loading = true;
          if (this.isAdd) {
            this.doAdd();
          } else {
            this.doEdit();
          }
        }
      });
    },
    doAdd() {
      contact
        .addOrEdit(this.form)
        .then((res) => {
          this.resetForm();
          this.loading = false;
          this.sup_this.init();
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    doEdit() {
      contact
        .addOrEdit(this.form)
        .then((res) => {
          this.resetForm();
          this.loading = false;
          this.sup_this.init();
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    resetForm() {
      this.hiddenDialog();
      this.$refs["form"].resetFields();
    },
    changeData(busId) {
      const data = { paramBean: { id: busId } };
      contact
        .toChange(data)
        .then((res) => {
          this.displayDialog();
          this.form = res.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    setObjectIdAndCode() {
      const _this = this;
      const busMainData = _this.sup_this.busMainData;
      if (busMainData) {
        _this.form.busMainData.objectId = busMainData.id;
        _this.form.busMainData.objectSysCode = busMainData.sysGenCode;
        _this.form.busMainData.busModule = busMainData.busModule;
      }
    },
  },
};
</script>
